

#import "SharedViewController.h"
#import "patternView.h"

@interface SharedViewController ()

@property(nonatomic,weak) UIView *cover;
@property(nonatomic,weak) patternView *patten;


//动画
@property (nonatomic, weak)CAEmitterLayer *explosionLayer;
@property (nonatomic, weak)CAEmitterLayer *snowEmitter;

//背景
@property (nonatomic, weak)UIImageView *imageV;

@end

@implementation SharedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self showSnow];
}

#pragma mark
#pragma mark - 下雪动画
- (void)showSnow{
    
    
    
    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];
    
     [self.view insertSubview:_imageV belowSubview:(id)snowEmitter];
    
    snowEmitter.emitterPosition = CGPointMake(self.view.bounds.size.width / 2.0, -30);
    
    snowEmitter.emitterSize  = CGSizeMake(self.view.bounds.size.width * 2.0, 0.0);
    
    snowEmitter.emitterShape = kCAEmitterLayerLine;
    snowEmitter.emitterMode  = kCAEmitterLayerOutline;
    
    CAEmitterCell *snowflake = [CAEmitterCell emitterCell];
    
    
    snowflake.birthRate	= 1.0;
    snowflake.lifetime	= 120.0;
    snowflake.velocity	= -10;
    snowflake.velocityRange = 10;
    snowflake.yAcceleration = 2;
    snowflake.emissionRange = 0.5 * M_PI;
    snowflake.spinRange = 0.25 * M_PI;
    
    
    snowflake.contents  =(id) [UIImage imageNamed:@"snow2"].CGImage;
    //    snowflake.color	= [[UIColor colorWithRed:0.600 green:0.658 blue:0.743 alpha:1.000] CGColor];
    
    
    snowEmitter.shadowOpacity = 1.0;
    snowEmitter.shadowRadius  = 0.0;
    snowEmitter.shadowOffset  = CGSizeMake(0.0, 1.0);
    snowEmitter.shadowColor   = [[UIColor whiteColor] CGColor];
    
    snowEmitter.emitterCells = [NSArray arrayWithObject:snowflake];
    [self.imageV.layer insertSublayer:snowEmitter atIndex:0];
    

    
}

- (void)setupUI{
    
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(shareToOthers)];
    
     [shareBtn setTintColor:[UIColor blackColor]];
    self.navigationItem.rightBarButtonItem = shareBtn;
    
    //背景
    self.view.backgroundColor = [UIColor grayColor];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.view.frame];
    
    self.imageV = imageV;
    
    imageV.center = self.view.center;
    
    imageV.contentMode = UIViewContentModeScaleToFill;
    
    imageV.image = [UIImage imageNamed:@"ssnow"];
    
    [self.view addSubview:imageV];
    
   
    
}

- (void)shareToOthers{
    
    if (self.patten) {
        return;
    }
    //添加遮罩视图
    UIView *cover = [[UIView alloc] initWithFrame:self.view.bounds];
    cover.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [self.view addSubview:cover];
    self.cover = cover;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap)];
    [_cover addGestureRecognizer:tap];
    
    patternView *pattern = [patternView patternView];
    _patten = pattern;
    [self.view addSubview:pattern];
    pattern.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 150);
    [UIView animateWithDuration:0.5 animations:^{
//        pattern.frame = CGRectMake(0, 517, 375, 150);
        
        CGRect tempF = pattern.frame;
        
        tempF.origin.y = [UIScreen mainScreen].bounds.size.height - 150;
        
        pattern.frame = tempF;
    }];
}


- (void)clickTap{

    [UIView animateWithDuration:.5 animations:^{
        //1. 让 patten 下去
        CGRect tempF = self.patten.frame;
        tempF.origin.y = [UIScreen mainScreen].bounds.size.height+150;
        self.patten.frame = tempF;
    } completion:^(BOOL finished) {
        //2. 让patten消失
        [self.patten removeFromSuperview];
        self.patten = nil;
        [self.cover removeFromSuperview];
        self.cover = nil;
    }];
    
    //3.

    

}


@end
